package com.sdp.wx.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_vehicle_wz.entity.WxRepairVehicleWz;
import com.sdp.wx.db.wx_wz_output.entity.WxWzOutput;
import com.sdp.wx.db.wx_wz_output.service.IWxWzOutputService;
import com.sdp.wx.db.wx_wz_output_detail.entity.WxWzOutputDetail;
import com.sdp.wx.handleImpl.WzInventoryOutputHandlerImpl;
import com.sdp.wx.handleImpl.WzRepairOutPutHandleImpl;
import com.sdp.wx.handleImpl.WzScrapOutPutHandleImpl;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/WxWzOutputController")
public class WxWzOutputController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxWzOutputService iWxWzOutputService;
	
	@Autowired
	private WzRepairOutPutHandleImpl wzRepairOutPutHandleImpl;
	
	@Autowired
	private WzScrapOutPutHandleImpl WzScrapOutPutHandleImpl;
	
	@Autowired
	private WzInventoryOutputHandlerImpl wzInventoryOutputHandlerImpl;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxWzOutputService, WxWzOutput.class, dataCenter));
	}
	
	@PostMapping(value = "/repairOutPut")
	public Result<?> repairOutPut(@RequestBody JSONObject obj) {
		if(obj==null || obj.get("formData")==null || obj.get("wzList")==null){
			return Result.error("领料信息不存在");
		}
		List<WxRepairVehicleWz> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxRepairVehicleWz.class);
		WxWzOutput wxWzOutput = JSONObject.parseObject(JSON.toJSONString(obj.get("formData")), WxWzOutput.class);
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WxWzOutput.class, wxWzOutput);
		businessContext.put(WxRepairVehicleWz.class, wzList);
		wzRepairOutPutHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wxWzOutput);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
	
	@PostMapping(value = "/scrapOutPut")
	public Result<?> scrapOutPut(@RequestBody JSONObject obj) {
		if(obj==null || obj.get("formData")==null || obj.get("wzList")==null){
			return Result.error("出库信息不存在");
		}
		List<WxWzOutputDetail> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxWzOutputDetail.class);
		WxWzOutput wxWzOutput = JSONObject.parseObject(JSON.toJSONString(obj.get("formData")), WxWzOutput.class);
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WxWzOutput.class, wxWzOutput);
		businessContext.put(WxWzOutputDetail.class, wzList);
		WzScrapOutPutHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wxWzOutput);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
	
	@PostMapping(value = "/inventoryOutput")
	public Result<?> inventoryOutput(@RequestBody JSONObject obj) {
		if(obj==null || obj.get("formData")==null || obj.get("wzList")==null){
			return Result.error("出库信息不存在");
		}
		List<WxWzOutputDetail> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxWzOutputDetail.class);
		WxWzOutput wxWzOutput = JSONObject.parseObject(JSON.toJSONString(obj.get("formData")), WxWzOutput.class);
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WxWzOutput.class, wxWzOutput);
		businessContext.put(WxWzOutputDetail.class, wzList);
		wzInventoryOutputHandlerImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wxWzOutput);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
	
}

